﻿#ifndef DDSHEETVIEW_H
#define DDSHEETVIEW_H

#include "SheetDeclare.h"
#include <QWidget>
#include "BRAnyChatCoreSDK.h"

#include "AnyChatCoreSDK.h"

#include "SheetGlobal.h"
#include <qtsoap.h>

#include <QNetworkAccessManager>
#include <QUrl>
#include <QFile>
#include <QMediaPlayer>

namespace Ui {class SheetView;};

#define DEMO_SHOW_USER_NUM		4						//< 定义在demo程序中显示用户面板的数量

enum WEBSERVICE_TYPE
{
	SERVICE_LOGIN = 0,
	SERVICE_APPLY = 1,
	SERVICE_PREVIEW = 2,
	SERVICE_PLAYBACK = 3,
	SERVICE_ONLYLOHIN = 4,
	SERVICE_GETTOKEN = 5,
};

struct SHEETCAMINFO
{
	QString strBeginTime;
	QString strEndTime;
	QString strCamIndexCode;
};

class SheetView : public QWidget
{
	Q_OBJECT

public:
	SheetView(int sheetid, int sheetBox, QWidget *parent = 0);
	~SheetView();

public:
	void initialise();
	void initConnect();
	void userLogin();
	void enterRoom();
	void leaveRoom();

	LRESULT OnGVClientConnect(WPARAM wParam, LPARAM lParam);
	LRESULT OnGVClientLogin(WPARAM wParam, LPARAM lParam);
	LRESULT OnGVClientEnterRoom(WPARAM wParam, LPARAM lParam);
	LRESULT OnGVClientOnlineUser(WPARAM wParam, LPARAM lParam);
	LRESULT OnGVClientMicStateChange(WPARAM wParam, LPARAM lParam);
	LRESULT OnGVClientUserAtRoom(WPARAM wParam, LPARAM lParam);
	LRESULT OnGVClientLinkClose(WPARAM wParam, LPARAM lParam);
	LRESULT OnGVClientChatModeChange(WPARAM wParam, LPARAM lParam);

	// 用户摄像头状态改变消息
	virtual void OnAnyChatCameraStateChgMessage(DWORD dwUserId, DWORD dwState) {}
	// 用户活动状态发生变化消息
	virtual void OnAnyChatActiveStateChgMessage(DWORD dwUserId, DWORD dwState) {}
	// P2P连接状态变化消息
	virtual void OnAnyChatP2PConnectStateMessage(DWORD dwUserId, DWORD dwState){}
	// 用户私聊请求消息
	virtual void OnAnyChatPrivateRequestMessage(DWORD dwUserId, DWORD dwRequestId) {}
	// 用户私聊请求回复消息
	virtual void OnAnyChatPrivateEchoMessage(DWORD dwUserId, DWORD dwErrorCode) {}
	// 用户退出私聊消息
	virtual void OnAnyChatPrivateExitMessage(DWORD dwUserId, DWORD dwErrorCode) {}
	// SDK警告消息
	virtual void OnAnyChatSDKWarningMessage(DWORD dwErrorCode, DWORD dwReserved) {}
	// 异步消息通知回调函数
	virtual void OnAnyChatNotifyMessageCallBack(DWORD dwNotifyMsg, DWORD wParam, DWORD lParam) {}

	void static CALLBACK SDKFilterData_CallBack(LPBYTE lpBuf, DWORD dwLen, LPVOID lpUserValue);
	void static CALLBACK TextMessage_CallBack(DWORD dwFromUserid, DWORD dwToUserid, BOOL bSecret, LPCTSTR lpMsgBuf, DWORD dwLen, LPVOID lpUserValue);
	void static CALLBACK NotifyMessage_CallBack(DWORD dwNotifyMsg, DWORD wParam, DWORD lParam, LPVOID lpUserValue);

protected:

	void timerEvent(QTimerEvent *event);

protected slots:
	void on_btnPlayBack_clicked();
	void on_btnClose_clicked();
	void on_btnStart_clicked();
	void on_btnPause_clicked();
	void on_btnEnterRoom_clicked();
	void on_btnUp_pressed();
	void on_btnUp_released();
	void on_btnDown_pressed();
	void on_btnDown_released();
	void on_btnLeft_pressed();
	void on_btnLeft_released();
	void on_btnRight_pressed();
	void on_btnRight_released();

	void on_btnLeftUp_pressed();
	void on_btnLeftUp_released();
	void on_btnLeftDown_pressed();
	void on_btnLeftDown_released();
	void on_btnRightUp_pressed();
	void on_btnRightUp_released();
	void on_btnRightDown_pressed();
	void on_btnRightDown_released();

	void on_btnZoomIn_pressed();
	void on_btnZoomIn_released();
	void on_btnZoomOut_pressed();
	void on_btnZoomOut_released();
	void on_btnFocusNear_pressed();
	void on_btnFocusNear_released();
	void on_btnFocusFar_pressed();
	void on_btnFocusFar_released();
	void on_btnIrisOpen_pressed();
	void on_btnIrisOpen_released();
	void on_btnIrisClose_pressed();
	void on_btnIrisClose_released();

	void getResponse();
	void NotifyPlatformplaybackctrl(int event_type, int wnd_index, int lparam, QString sparam);

	void httpFinished();//下载录音
	void httpReadyRead();

private:
	void ptzControl(int type, QString strAction, QString strPriorit);
	void initAxWidgetSheet();
	void initPagePreview();
	void initPagePlayBack();
	void changePreview(QString strCamIndexCode);
	void initSheetCamInfo();

	void service_sdkLogin(WEBSERVICE_TYPE iServiceType);
	void service_applyToken(QString tgt, WEBSERVICE_TYPE iServiceType);
	void service_getPreviewOcxOptions(QString token, QString cameraIndexCode);
	void service_getPlaybackOcxOptions(QString token, QString cameraIndexCode, QString beginTime, QString endTime, QString storeDeviceType);

	void response_sdkLogin(QString strReturn);
	void response_applyToken(QString strReturn);
	void response_getPreviewOcxOptions(QString strReturn);
	void response_getPlaybackOcxOptions(QString strReturn);
	void response_setToken(QString strReturn);

	void startRequest();
	QString createVideoPath(QString fileName,bool bURL);
	void videoDownPlay();

signals:
	void closeSheetSignal(int);

private:
	int iSheetId; //操作票id
	int iSheetBox;

	int iTimerId;//定时器id
	QRecord dsRecordTimer;
	int iCurrentRow;
	QString strPreviousSelectText;//前一条数据
	QString strStartTime;
	QTime tCurBeginTime;//当前条开始时间
	QString hikTgt;//用户票据
	QList<SHEETCAMINFO> listSheetCamInfo;
	int tokenId;

	QStringList listLY;//录音
	QStringList listLYDown;//需要下载的录音
	QNetworkAccessManager qnam;
	QNetworkReply *pReply;
	QFile *pFile;
	QMediaPlayer *pMusicPlayer;
	QMediaPlaylist *playList;

	QString strCurrentCamIndex;//当前监控点编号

	Ui::SheetView *ui;

	CAnyChatCoreSDK m_AnyChatSDK;
	QString str;
	int x1, x2, y1, y2;
	QString m_strLogInfo;
	bool m_bEnableAGC;    //AGC（自动增益）
	bool m_bEnableEcho;   //AEC （回音消除）
	bool m_bEnableNS;      //NS（噪音抑制）
	bool m_bEnableVAD;   //VAD（静音检测）

	QRect  m_UserRect[DEMO_SHOW_USER_NUM];     //用户显示位置
	int m_iUserID[DEMO_SHOW_USER_NUM];              //用户ID号

	int m_dwTransTaskId;                                                       // 传输任务编号

	//webservice接口
	QtSoapHttpTransport http;
	WEBSERVICE_TYPE serviceType;
};

#endif // DDSHEETVIEW_H
